package com.mxt.moviedemo.service.impl;

import com.mxt.moviedemo.controller.InfoController;
import com.mxt.moviedemo.dao.ActorsMapper;
import com.mxt.moviedemo.entity.Actors;
import com.mxt.moviedemo.entity.Directors;
import com.mxt.moviedemo.entity.PageRequest;
import com.mxt.moviedemo.entity.PageResult;
import com.mxt.moviedemo.service.ActorsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActorsServiceImpl  implements ActorsService {
    @Autowired
    private ActorsMapper actorsMapper;

    @Override
    public PageResult<Actors> searchActorsByKeyword(PageRequest pageRequest, String keyword) {
        List<Actors> actors = actorsMapper.searchActorsByKeyword(pageRequest, keyword);
        int total = actorsMapper.getTotalActorsByKeyword(keyword);
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, actors);
    }

    @Override
    public Actors getActorById(int aid) {
//        System.out.println(aid);
        Actors actor = actorsMapper.getActorById(aid);
        return actor;

    }

    @Override
    public PageResult<Actors> getActorsByPage(PageRequest pageRequest) {
        List<Actors> actors = actorsMapper.getActorsByPage(pageRequest);
        // 过滤掉null元素
        List<Actors> nonNullActors = new ArrayList<>();
        for (Actors actor : actors) {
            if (actors != null) {
                nonNullActors.add(actor);
            }
        }
        int total = actorsMapper.getTotalActors();
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, nonNullActors);
    }

    @Override
    public List<Actors> searchActorsByName(String aname) {
        return actorsMapper.searchActorsByName(aname);
    }

    @Override
    public int addActors(Actors actors) {
        return actorsMapper.addActors(actors);
    }

    @Override
    public boolean updateActors(Actors actors) {
        return actorsMapper.updateActors(actors);
    }

    @Override
    public int deleteActors(int aid) {
        return actorsMapper.deleteActors(aid);
    }
}